package com.ltw.modules.test.service.impl;

import com.ltw.modules.common.entity.Result;
import com.ltw.modules.common.entity.SearchBean;
import com.ltw.modules.common.entity.SearchBean;
import com.ltw.modules.test.dao.StudentDao;
import com.ltw.modules.test.entity.Student;
import com.ltw.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @Description StudentServiceImpl
 * @Author HymanHu
 * @Date 2021/9/14 14:58
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    @Override
    @Transactional
    public Result<Student> insertStudent(Student student) {
//        studentDao.saveAndFlush(student);
        studentDao.insertStudent(student);
        return new Result<>(Result.ResultCode.SUCCESS.code,
                "Insert success.", student);
    }

    @Override
    @Transactional
    public Result<Student> updateStudent(Student student) {
        studentDao.saveAndFlush(student);
        return new Result<>(Result.ResultCode.SUCCESS.code,
                "Update success.", student);
    }

    @Override
    @Transactional
    public Result<Object> deleteStudentByStudentId(int studentId) {
        studentDao.deleteById(studentId);
        return new Result<>(Result.ResultCode.SUCCESS.code,
                "Delete success.");
    }

    @Override
    public Student getStudentByStudentId(int studentId) {
        return studentDao.findById(studentId).orElseGet(null);
    }

    @Override
    public Student findTopByStudentNameAndEmail(String studentName, String email) {
        return studentDao.findTopByStudentNameAndEmail(studentName, email);
    }

    @Override
    public Page<Student> getStudentsBySearchBean(SearchBean searchBean) {
        searchBean.initSearchBean();

        // 准备 Direction
        Direction direction = searchBean.getDirection().toLowerCase().equals("asc") ?
                Direction.ASC : Direction.DESC;
        // 准备 Sort
        Sort sort = Sort.by(direction, searchBean.getOrderBy());
        // 准备 PageRequest
        PageRequest pageRequest = PageRequest.of(
                searchBean.getCurrentPage() - 1, searchBean.getPageSize(), sort);

        // 准备 student 对象，将查询关键字设置到 student 对应的属性上（用什么属性查询，则设置什么属性）
        Student student = new Student();
        student.setStudentName(searchBean.getKeyword());
        student.setEmail(searchBean.getKeyword());
        // 准备 ExampleMatcher 对象
        ExampleMatcher exampleMatcher = ExampleMatcher
                .matchingAny()
                .withMatcher("studentName", matcher -> matcher.contains())
                .withMatcher("email", matcher -> matcher.contains())
                .withIgnorePaths("id", "age");
        // 准备 Example 对象
        Example<Student> example = Example.of(student, exampleMatcher);

        // 调用接口 findAll(Example<S> var1, Pageable var2) 方法，返回 page 对象
        return studentDao.findAll(example, pageRequest);
    }

    @Override
    public List<Student> getStudentsByStudentNameOrEmail(String studentName, String email) {
        return Optional
                .ofNullable(studentDao.getStudentsByStudentNameOrEmail(studentName, email))
                .orElse(Collections.emptyList());
    }
}
